/*
Copyright (c) 2010 Neil Wood

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package envisage.Managers
{
	import envisage.Controls.Desktop;
	import envisage.Controls.DropEvent;
	import Util.Utils;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	
	public class DropManager
	{
		public static function AllowDrag(d:DisplayObject, data:Object):void
		{
			if( data != null )
			{
				d.addEventListener(MouseEvent.MOUSE_DOWN, OnMouseDown, false, 0, true);
				dictObjectToData[d] = data;		
			}
			
			else
			{
				d.removeEventListener(MouseEvent.MOUSE_DOWN, OnMouseDown, false);
				delete dictObjectToData[d]; 
			}
			
		}
		
		private static var mouseDownObject:DisplayObject = null;
		private static var data:Object;
		private static var lastOver:Array = null;
		private static var dictObjectToData:Dictionary = new Dictionary(true);
		
		private static var move:int = 0;
		
		private static function OnMouseDown(e:MouseEvent):void
		{
			mouseDownObject = e.currentTarget as DisplayObject;
		
			mouseDownObject.stage.addEventListener(MouseEvent.MOUSE_UP, OnMouseUp);
			mouseDownObject.stage.addEventListener(MouseEvent.MOUSE_MOVE, OnMouseMove);
			
			move = 1;
		}
		
		private static function UpdateFloat(stage:Stage):void
		{
			DropEvent.float.x = stage.mouseX;
			DropEvent.float.y = stage.mouseY;
		}
		
		private static function CreateFloat(d:DisplayObject):DisplayObject
		{
			var bd:BitmapData = new BitmapData(d.width, d.height);
			bd.draw(d);
			
			var s:Sprite = new Sprite();
			s.addChild(new Bitmap(bd));
			
			s.alpha = 0.5;
			
			return s;
		}
		
		private static function OnMouseUp(e:MouseEvent):void
		{
			var d:DisplayObject = e.currentTarget as DisplayObject;
			
			d.stage.removeEventListener(MouseEvent.MOUSE_UP, OnMouseUp);
			d.stage.removeEventListener(MouseEvent.MOUSE_MOVE, OnMouseMove);
			
			if( move == 2 )
			{
				var i:int;
			
				for(i = 0 ; i < lastOver.length ; i++)
					DisplayObject(lastOver[i]).dispatchEvent(new DropEvent(DropEvent.DROP_OUT, data));
			
				lastOver = null;
			
				var over:Array = GetObjects(d.stage);
						
				for(i = 0 ; i < over.length ; i++)
					DisplayObject(over[i]).dispatchEvent(new DropEvent(DropEvent.DROPPED, data));
			
				data = null;
			
				Desktop.overlay.removeChild(DropEvent.float);
			}
			
			move = 0;
		}
		
		private static function OnMouseMove(e:MouseEvent):void
		{
			if( move == 0 ) return;
			
			var d:DisplayObject = e.currentTarget as DisplayObject;
			
			if( move == 1 )
			{
				data = dictObjectToData[mouseDownObject];
				lastOver = [];
			
				DropEvent.float = null;
				mouseDownObject.dispatchEvent(new DropEvent(DropEvent.DRAGGING, data));
			
				if( DropEvent.float == null ) DropEvent.float = CreateFloat(mouseDownObject);
			
				Desktop.overlay.addChild(DropEvent.float);
				UpdateFloat(d.stage);
			
				move = 2;
			}
			
			var over:Array = GetObjects(d.stage);
			
			var i:int;
			var o:DisplayObject;
			
			for(i = 0 ; i < over.length ; i++)
			{
				o = over[i];
				if( lastOver.indexOf(o) == -1 )
				{
					 o.dispatchEvent(new DropEvent(DropEvent.DROP_OVER, data));
				}
			}
			
			for(i = 0 ; i < lastOver.length ; i++)
			{
				o = lastOver[i]; 
				if( over.indexOf(o) == -1 ) o.dispatchEvent(new DropEvent(DropEvent.DROP_OUT, data));
			}
			
			lastOver = over;
			
			UpdateFloat(d.stage);
		}
		
		private static function GetObjects(stage:Stage):Array
		{
			var arr:Array = stage.getObjectsUnderPoint(new Point(stage.mouseX, stage.mouseY));
	
			var len:int = arr.length;
			
			for(var i:int = 0 ; i < len ; i++)
			{
				var d:DisplayObject = arr[i];
				
				d = d.parent;	
				
				while(d != null )
				{
					arr.push(d);
					d = d.parent;
				}
			}
			
			for(var a:int = 0 ; a < arr.length ; a++)
				for(var b:int = 0 ; b < arr.length ; b++)
					if( (a != b) && arr[a] == arr[b] )
					{
						Utils.ArrayDeleteIndex(arr, b);
						b--;
					}
			
			return arr;
		}
		
	}
}






